package com.invite.me.wigets;

import android.content.Context;
import android.util.Log;

import com.invite.me.app.MyApplication;
import com.invite.me.wlqq.Constants;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengMessageHandler;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.message.inapp.InAppMessageManager;

public class UmPushHelper {
    public static final String TAG = "UmPushHelper";

    static UmPushHelper umPushHelper;
    Context context = MyApplication.getMyApplication();
    private PushAgent mPushAgent;

    public static UmPushHelper getInstance() {
        if (null == umPushHelper) {
            umPushHelper = new UmPushHelper();
        }
        return umPushHelper;
    }

    /**
     * 友盟初始化
     * <p>
     * 友盟初始化及相关配置
     * 注册
     * 通道配置
     */
    public void init() {

        UMConfigure.init(context, Constants.UmAppKey, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, Constants.UmSecretkey);

        //获取消息推送代理示例
        PushAgent mPushAgent = PushAgent.getInstance(context);
        UMConfigure.setLogEnabled(true);//日志输出，上线关闭
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i(TAG, "注册成功：deviceToken：-------->  " + deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {
                Log.e(TAG, "注册失败：-------->  " + "s:" + s + ",s1:" + s1);
            }
        });
        // 打开统计SDK调试模式
        // UMConfigure.setLogEnabled(true);

//        PlatformConfig.setWeixin("wx50440a59c2f20f3e", "a46b7c8c722f187cea2fe6156058ef55");
//        PlatformConfig.setQQZone("101576985", "e6ec07de4e214cd01a958b6065fa76f7");
//        PlatformConfig.setSinaWeibo("2811132424", "b8aa7d51557e5bb05e75365837326bf1", "http://www.heyjoy.cc");
//
//        //OPPO通道，参数1为app key，参数2为app secret
//        OppoRegister.register(context, "984953b45b514a2e9873150c0b8a89e9", "b8494e83d75445c58b53a046253c0999");
//        //小米
//        MiPushRegistar.register(context, "2882303761518007862", "5261800755862");
//        //华为
//        HuaWeiRegister.register(MyApplication.getMyApplication());
//        //魅族
//        MeizuRegister.register(context, "129565", "8d471f0e008408bbf99b40718ea54b6");

        //vivo 通道
//        VivoRegister.register(context);
        mPushAgent.onAppStart();
        //最多展示条数
        mPushAgent.setDisplayNotificationNumber(10);
        //通知或消息接收
        setMessageHandler();
        //通知点击
        setNotificationClickHandler();

        InAppMessageManager.getInstance(context).setInAppMsgDebugMode(true);
    }
    /**
     * MessageHandler有很多回调方法，根据自己需要选择
     */
    private void setMessageHandler() {
        UmengMessageHandler messageHandler = new UmengMessageHandler() {
            @Override
            public void dealWithNotificationMessage(Context context, UMessage uMessage) {
                super.dealWithNotificationMessage(context, uMessage);
                Log.e(TAG, "um notification msg.extra" + uMessage.extra);

                /**
                 * 应用不在前台时，不弹框，并把推送的数据存起来
                 *
                 * 在前台时直接弹框
                 */

             /*   Intent intent = new Intent(context, HomeActivity.class);
                // intent.putExtra("bean", bean);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);*/
            }
        };
        PushAgent.getInstance(context).setMessageHandler(messageHandler);
    }
    private void setNotificationClickHandler() {
        /**
         * 自定义行为的回调处理，参考文档：高级功能-通知的展示及提醒-自定义通知打开动作
         * UmengNotificationClickHandler是在BroadcastReceiver中被调用，故
         * 如果需启动Activity，需添加Intent.FLAG_ACTIVITY_NEW_TASK
         * */
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {

            @Override
            public void launchApp(Context context, UMessage msg) {

                starNews(msg);
                Log.e("umenguuu", "helper launchApp extra:" + msg.extra.toString());
                super.launchApp(context, msg);
            }

            @Override
            public void openUrl(Context context, UMessage msg) {
                Log.e("umenguuu", "helper openUrl extra:" + msg.extra.toString());
                super.openUrl(context, msg);
            }

            @Override
            public void openActivity(Context context, UMessage msg) {
                //获取extra字段值
                Log.e("umenguuu", "helper openActivity extra:" + msg.extra.toString());
               /* Intent intent = null;

                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);*/
            }

            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                Log.e("umenguuu", "helper dealWithCustomAction extra:" + msg.extra.toString());
            }
        };
        //使用自定义的NotificationHandler
        PushAgent.getInstance(context).setNotificationClickHandler(notificationClickHandler);
    }

    public void starNews(UMessage msg) {



    }



}
